package com.langxin.site.biz;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.type.TypeReference;
import com.langxin.site.util.constant.SiteConstant;
import com.langxin.site.util.web.ActionUtils;
import com.langxin.site.util.web.CookieUtil;
import com.langxin.site.util.web.JsonUtils;

/**
 * 业务basebiz
 * @author guqj
 *
 */
public class BaseBiz {

	/**
	 * 判断是否有阅读过
	 * @param type  旅游/资讯...
	 * @param id
	 * @return
	 */
	public boolean isRead(String type, Integer id){
		// 从cookie中读取记录
		String record = CookieUtil.getCookieValue(ActionUtils.getRequest(), SiteConstant.CookieKey.ARTIC_READ.getValue());
		// 数据转换
		Map<String,List<Integer>> recordMap = JsonUtils.fromJson(record, new TypeReference<Map<String,List<Integer>>>() {});
		// 验证用户是否有浏览过
		List<Integer> ids = null;
		if(recordMap != null){
			if(recordMap.containsKey(type)){
				ids = recordMap.get(type);
				for(Integer cid : ids){
					if(cid.compareTo(id) == 0){
						// 说明已阅读过
						return true;
					}
				}
				// 将ID加入cookie
				ids.add(id);
			}else{
				ids = Arrays.asList(id);
			}
		}else{
			recordMap = new HashMap<String, List<Integer>>();
			ids = Arrays.asList(id);
		}
		recordMap.put(type, ids);
		CookieUtil.createCookie(ActionUtils.getResponse(), SiteConstant.CookieKey.ARTIC_READ.getValue(), JsonUtils.toJson(recordMap), SiteConstant.COOKIE_DOMAIN, "/");
		return false;
	}
	
}
